package io.github.uicdb.et.data;

import io.github.uicdb.et.ElementalTheory;
import io.github.uicdb.et.ModItems;
import io.github.uicdb.et.ModTags;
import io.github.uicdb.et.item.curios.CurioItem;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.tags.ItemTagsProvider;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.data.event.GatherDataEvent;
import org.jetbrains.annotations.NotNull;
import top.theillusivec4.curios.api.CuriosApi;

import java.util.ArrayList;
import java.util.Map;

class ETItemTagsProvider extends ItemTagsProvider {
    public ETItemTagsProvider(GatherDataEvent event, ETBlockTagsProvider blockTags) {
        super(event.getGenerator().getPackOutput(), event.getLookupProvider(), blockTags.contentsGetter());
    }

    @Override
    protected void addTags(HolderLookup.@NotNull Provider provider) {
        var neckLace = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "necklace"));
        var back = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "back"));
        var hat = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "hat"));
        var bracelet = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "bracelet"));
        var ring = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "ring"));
        var medicine_bottle = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "medicine_bottle"));
        var curio = TagKey.create(BuiltInRegistries.ITEM.key(), ResourceLocation.fromNamespaceAndPath(CuriosApi.MODID, "curio"));

        tag(bracelet).add(ModItems.RANDOM_CURIO.value()).add(ModItems.SUNDEE.asItem());
        tag(back).add(ModItems.WIND_WING.value()).add(ModItems.THE_ENCHANTING_SCROLL.asItem()).add(ModItems.MAPLE_SORROW.asItem()).add(ModItems.DOGE_HAPPY.asItem());
        tag(ring).add(ModItems.PARALYSIS_RING.asItem());
        tag(hat).add(ModItems.SAFTY_HAT.asItem()).add(ModItems.WHEAT_WAVE.asItem());
        tag(neckLace)
                .add(ModItems.LUNAR_ECLIPSE.asItem())
                .add(ModItems.SUNLIT.asItem())
                .add(ModItems.ENDER_LOCK.asItem())
                .add(ModItems.SHADOW_NECKLACE.asItem());
        tag(ModTags.PR_CRYSTEL).add(ModItems.PR_CRYSTEL.asItem());
        tag(medicine_bottle).add(ModItems.DST.asItem());
        for (Map.Entry<ResourceKey<Item>, Item> entry : BuiltInRegistries.ITEM.entrySet()) {
            var loc = entry.getKey().location();
            if (loc.getNamespace().equals(ElementalTheory.MODID) && entry.getValue().asItem() instanceof CurioItem) {
                tag(curio).add(entry.getKey());
            }
        }
        var cache = new ArrayList<TagKey<Item>>();
        for (Item item : BuiltInRegistries.ITEM) {
            if (item instanceof CurioItem curioItem) {
                var t = curioItem.getEquippedTag();
                if (!cache.contains(t)) {
                    cache.add(t);
                    tag(t).add(item);
                }
            }
        }
        tag(ModTags.ELEMENT_WEAPON).addOptionalTag(ItemTags.SWORDS).addOptionalTag(ItemTags.AXES).addOptionalTag(ItemTags.WEAPON_ENCHANTABLE);
    }
}
